/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.seehealth.healthpad.chart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

/**
 * Sales demo bar chart.
 */
public class HealthBarChart extends AbstractChart {

  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "hypertension horizontal chart";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "一周高血压趋势图";
  }

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  public GraphicalView execute(Context context, Map<String, Object> params) {
	  GraphicalView view = null;
		if(params!=null&&params.size()!=0){
			String[] titles = (String[]) params.get("titles");
			List<double[]> values = (List<double[]>) params.get("datas");
			List<String> dates = (List<String>) params.get("dates");
			String XName = params.get("XName").toString();
			String YName = params.get("YName").toString();
			String name = params.get("name").toString();
			int range = (Integer) params.get("range");
			List<double[]> x = new ArrayList<double[]>();
			int[] colors = new int[] { Color.YELLOW };
			PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };
			XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
			int length = renderer.getSeriesRendererCount();
			for (int i = 0; i < length; i++) {
				((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
						.setFillPoints(true);
			}
			if (values!=null&&values.size()==0) {
				double [] temp = new double[]{ -10, -10, -10 };
				values.add(temp);
				dates.add("");
			}

			setChartSettings(renderer, name,YName,XName, 0.0, 6, 0,
					range + 10, Color.LTGRAY, Color.LTGRAY);

			renderer.setXLabels(1);
			renderer.setYLabels(10);
			if (dates != null && dates.size() != 0) {
				for (int i = 1; i <= dates.size(); i++) {
					renderer.addXTextLabel(i, dates.get(i - 1));
				}
			}
			renderer.setShowGrid(true);
			renderer.setXLabelsAlign(Align.RIGHT);
			renderer.setYLabelsAlign(Align.RIGHT);
			renderer.setZoomButtonsVisible(true);
			renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
			renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
			renderer.setApplyBackgroundColor(true);
			renderer.setBackgroundColor(Color.rgb(51, 102, 205));
			renderer.setMarginsColor(Color.rgb(51, 102, 205));
			//renderer.setPanEnabled(true, true);
			renderer.setPanEnabled(true);
			length = renderer.getSeriesRendererCount();
			for (int i = 0; i < length; i++) {
				SimpleSeriesRenderer seriesRenderer = renderer
						.getSeriesRendererAt(i);
				seriesRenderer.setDisplayChartValues(true);
			}

			view = ChartFactory.getLineChartView(context,
					buildBarDataset(titles, values), renderer);
			
		}
		return view;
  }

}
